package com.hcbd.tdwt.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class MoveImgView extends View {
    //圆的初始位置
    private int x = 100;
    private int y = 100;
    Context context;
    private int parent_width;
    private int parent_high;
    private Bitmap bitmap;
    private View view;
    /**
     * 有style资源文件时调用
     * @param context
     * @param attrs
     * @param defStyle
     */
    public MoveImgView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }
    /**
     * xml创建时调用
     * @param context
     * @param attrs
     */
    public MoveImgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        parent_width = manager.getDefaultDisplay().getWidth();
        parent_high = manager.getDefaultDisplay().getHeight();
    }
    /**
     * java代码创建时调用
     * @param context
     */
    public MoveImgView(Context context, Bitmap bitmap,View view) {
        super(context);
        this.context = context;
        this.bitmap=bitmap;
        this.view=view;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setTextSize(16);
        paint.setColor(Color.BLACK);
      //  Rect mDestRect = new Rect(50, 30, view.getWidth(), view.getHeight());
     //   canvas.drawBitmap(bitmap,null, mDestRect,paint);
        canvas.drawBitmap(bitmap, view.getLeft(), view.getTop(), paint);
        canvas.drawText("菜单",view.getLeft()+50, view.getTop()+100,  paint);
        // 画笔
        //   Paint paint = new Paint();
        //   paint.setColor(Color.RED);


        //   canvas.drawCircle(x, y, 20, paint);
    }
    float moveX;
    float moveY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                moveX = event.getX();
                moveY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                setTranslationX(getX() + (event.getX() - moveX));
                setTranslationY(getY() + (event.getY() - moveY));
                break;
            case MotionEvent.ACTION_UP:
                // 获取当前触摸点的x,y坐标
               /* x = (int) event.getX();
                y = (int) event.getY();
               */
                break;
        }
        /*//获取屏幕宽高
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = manager.getDefaultDisplay().getWidth();
        int heigh = manager.getDefaultDisplay().getHeight();

        //重新绘制圆 ,控制小球不会被移出屏幕
        if(x>=20 && y>=20 && x<=width-20 && y<=heigh-90){
            invalidate();
        }*/
        // 自己处理触摸事件
        return true;
    }

}
